/*********************************************************************************************************
* Movement in form of a cursor. Like in a 2d Car racing.
*
*  inspired by http://www.freeactionscript.com/2010/09/8-way-keyboard-movement-with-rotation-velocity/
*
*  Author of the ported code: Florian Weil - www.derhess.de
*
************************************************************************************************************/

inlets = 2;
outlets = 2;

// distance between two points or coordinate
var maxSpeed = 5;
var rotationSpeed = 5;
var rotationDirection = 0; // "-1" --> left, "0" --> no change, "1" --> right
var rotation = 0;
var thrust = .5;
var decay = .97;
var velocityDirection = 0; // "-1" --> slower, "0" --> no change, "1" --> faster
var currentSpeed = 0;
var speedX = 0;
var speedY = 0;



var canvas_width = 640;
var canvas_height = 480;

var x_pos = canvas_width/2;
var y_pos = canvas_height/2;



function setMaxSpeed(value)
{
	if(value > 0)
		maxSpeed = value;
	else
		post("ERROR: MaxSpeed value is negative. It must be greater than 0!\n");
}

function setRotationSpeed(value)
{
	if(value > 0)
		rotationSpeed = value;
	else
		post("ERROR: RotationSpeed value is negative. It must be greater than 0!\n");
}

function setVelocityDirection(value)
{
	if(value > -2 && value < 2)
		velocityDirection = value;
	else
		post("ERROR: VelocityDirection value is wrong. Value must be between -1 and 1!\n");
}

function setRotationDirection(value)
{
	if(value > -2 && value < 2)
		rotationDirection = value;
	else
		post("ERROR: RotationDirection value is wrong. Value must be between -1 and 1!\n");
}


function move() 
{
	// Rotate right or left
	rotation += (rotationSpeed * rotationDirection);
	
	if (velocityDirection == 1)
	{
		speedX += thrust * Math.sin(rotation * (Math.PI / 180));
		speedY += thrust * Math.cos(rotation * (Math.PI / 180));
	}
	else if (velocityDirection == -1){
		// Deccelerate when Up Arrow key is released
		speedX *= decay;
		speedY *= decay;
	}
	// Maintain speed limit
	currentSpeed = Math.sqrt((speedX * speedX) + (speedY * speedY));
	if (currentSpeed > maxSpeed)
	{
		speedX *= maxSpeed/currentSpeed;
		speedY *= maxSpeed/currentSpeed;
	}
	// Move _player based on calculations above
	y_pos -= speedY;
	x_pos += speedX;
}

function reset()
{
	maxSpeed = 5;
	rotationSpeed = 5;
	rotationDirection = 0; // "-1" --> left, "0" --> no change, "1" --> right
	rotation = 0;
	thrust = .5;
	decay = .97;
	velocityDirection = 0; // "-1" --> slower, "0" --> no change, "1" --> faster
	currentSpeed = 0;
	speedX = 0;
	speedY = 0;
	x_pos = canvas_width/2;
	y_pos = canvas_height/2;
}

//////////////////////////////////////////////////////////////////////////
//    MAX MSP FUNCTION
//////////////////////////////////////////////////////////////////////////
function bang()
{
	move();
	output();
}

//////////////////////////////////////////////////////////////////////////
//    Helper Functions
//////////////////////////////////////////////////////////////////////////
function output()
{
	outlet(0, x_pos);
    outlet(1, y_pos);
	//post("VelocityAngleAcceleration: Send Bang: " + x_pos + "/" + y_pos + "\n");
}
